package org.zn.note.jdk.others;

/**
 * 数字反序
 * 输入int 123，输出int 321
 * 不允许使用String、数组、List、Map等集合
 */
public class ReverseInt {

    public int reverse(int n) {
        int m = 0;
        int temp = 0;
        while (n > 0) {
            m = m * 10 + n % 10; // 通过%取余，拿出n的末位；这一轮m*10
            if (temp != m / 10) // 溢出反0
                return 0;
            temp = m;
            n = n / 10; // 通过/除法，降位
        }
        return m;
    }

    public static void main(String[] args) {
        ReverseInt reverseInt = new ReverseInt();
        for (int i = 1000; i < 1010; i++) {
            System.out.printf("%d %d\n", i, reverseInt.reverse(i));
        }
        int i = Integer.MAX_VALUE;
        System.out.printf("%d %d\n", i, reverseInt.reverse(i));
    }
}